﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
namespace IHISCETeim.DAL
{
	public class UReturnVisitDAL
	{
		public static bool Add(UReturnVisitInfo obj)
		{
			try
			{
				Database DB = DatabaseFactory.CreateDatabase();
				DbCommand dbCommand = DB.GetStoredProcCommand("UReturnVisit_Add",obj.UWCID,obj.KeepGood50,obj.OrderKeepGood50,obj.Guide,obj.Quality,obj.Usability_speed,obj.Usability_power,obj.Usability_oil,obj.Usability_keep,obj.Usability_space,obj.Usability_operation,obj.Usability_bug,obj.otherRemark,obj.WorkTime,obj.U4RID,
					obj.Usability_machine,
					obj.Usability_engine,
                    obj.Usability_electric,
                    obj.Usability_hydraumatic,
                    obj.Usability_air,
                    obj.Usability_physical,
                    obj.XianChang,
                    obj.QianZhi,
                    obj.JianJie
					);
				DB.ExecuteScalar(dbCommand);
				return true;
			}
			catch
			{
				return false;
			}

		}

		public static bool Update(UReturnVisitInfo obj)
		{
			Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UReturnVisit_Update", obj.UWCID, obj.KeepGood50, obj.OrderKeepGood50, obj.Guide, obj.Quality, obj.Usability_speed, obj.Usability_power, obj.Usability_oil, obj.Usability_keep, obj.Usability_space, obj.Usability_operation, obj.Usability_bug, obj.otherRemark, obj.WorkTime, obj.U4RID, obj.XianChang,
                    obj.QianZhi,
                    obj.JianJie);
			try
			{
				DB.ExecuteScalar(dbCommand);
				return true;
			}
			catch (Exception exc)
			{
				return false;
			}
		}
		public static bool Delete(int UWCID)
		{
			try
			{
				Database DB = DatabaseFactory.CreateDatabase();
				DbCommand dbCommand = DB.GetStoredProcCommand("UReturnVisit_Delete", UWCID);
				DB.ExecuteScalar(dbCommand);
				return true;
			}
			catch
			{
				return false;
			}
		}
		public static IDataReader GetByUWCID(int UWCID)
		{
			Database DB = DatabaseFactory.CreateDatabase();		
		
			DbCommand dbCommand = DB.GetStoredProcCommand("UReturnVisit_GetByUWCID", UWCID);
			return DB.ExecuteReader(dbCommand);
		}
        public static DataTable GetPagination(string CustomerName, string SerialNumber, string AgentName, string AgentAddress, string CreateDateFrom, string CreateDateTo, int PageIndex, int PageSize, string Sort, ref int TotalCount)
		{
			Database DB = DatabaseFactory.CreateDatabase();		
		
			DbCommand dbCommand = DB.GetStoredProcCommand("UReturnVisit_GetPagination");
			DB.AddInParameter(dbCommand, "CustomerName", DbType.String,CustomerName);
            DB.AddInParameter(dbCommand, "SerialNumber", DbType.String, SerialNumber);
			DB.AddInParameter(dbCommand, "AgentName", DbType.String,AgentName);
			DB.AddInParameter(dbCommand, "AgentAddress", DbType.String,AgentAddress);
			DB.AddInParameter(dbCommand, "CreateDateFrom", DbType.String,CreateDateFrom);
			DB.AddInParameter(dbCommand, "CreateDateTo", DbType.String,CreateDateTo);
			DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
			DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
			DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
			DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
			DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
			TotalCount=Convert.ToInt32(DB.GetParameterValue(dbCommand,"TotalCount"));
			return dt;
		}
		#region 报表
		/// <summary>
		/// 获取指定时间段内一个月交机回访报表数据
		/// </summary>
		/// <param name="CreateDateFrom"></param>
		/// <param name="CreateDateTo"></param>
		/// <returns></returns>
		public static DataTableCollection GetReport(string CreateDateFrom, string CreateDateTo,string VisitType)
		{
			Database DB = DatabaseFactory.CreateDatabase();

			DbCommand dbCommand = DB.GetStoredProcCommand("UReturnVist_GetReport");
			DB.AddInParameter(dbCommand, "CreateDateFrom", DbType.String, CreateDateFrom);
			DB.AddInParameter(dbCommand, "CreateDateTo", DbType.String, CreateDateTo);
            DB.AddInParameter(dbCommand, "VisitType", DbType.String, VisitType);
            DataTableCollection dt = DB.ExecuteDataSet(dbCommand).Tables;
			return dt;
		}
        /// <summary>
        /// 获取子问题的报表
        /// </summary>
        /// <param name="CreateDateFrom"></param>
        /// <param name="CreateDateTo"></param>
        /// <returns></returns>
        public static DataTableCollection GetVisitUsabilityReport(string CreateDateFrom, string CreateDateTo)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UReturnVist_GetVisitUsabilityReport");
            DB.AddInParameter(dbCommand, "CreateDateFrom", DbType.String, CreateDateFrom);
            DB.AddInParameter(dbCommand, "CreateDateTo", DbType.String, CreateDateTo);
            DataTableCollection dt = DB.ExecuteDataSet(dbCommand).Tables;
            return dt;
        }
		#endregion


		public static bool UReturnVisitUsabilityDetail_Add(List<UReturnVisitUsabilityDetailInfo> listVisit)
		{
            if (listVisit == null)
                return true;
            if (listVisit.Count <= 0)
                return true;
			Database DB = DatabaseFactory.CreateDatabase();
			using (DbConnection conn = DB.CreateConnection())
			{
				//打开连接
				conn.Open();
				//创建事务
				DbTransaction trans = conn.BeginTransaction();
				try
				{

					DbCommand dbCommandDel = DB.GetStoredProcCommand("UReturnVisitUsabilityDetail_Delete", listVisit[0].UWCID);
					DB.ExecuteScalar(dbCommandDel, trans);

					foreach (UReturnVisitUsabilityDetailInfo itm in listVisit)
					{

						DbCommand dbCommand = DB.GetStoredProcCommand("UReturnVisitUsabilityDetail_Add", itm.UWCID, itm.TempID);
						DB.ExecuteScalar(dbCommand, trans);
					}
					trans.Commit();
					return true;
				}
				catch
				{
					trans.Rollback();
					return false;
				}
				finally
				{
					conn.Close();
				}
			}
			
		}
		public static bool UReturnVisitUsabilityDetail_Delete(int UWCID)
		{
			Database DB = DatabaseFactory.CreateDatabase();

			DbCommand dbCommand = DB.GetStoredProcCommand("UReturnVisitUsabilityDetail_Delete", UWCID);
			try
			{
				DB.ExecuteScalar(dbCommand);
				return true;
			}
			catch
			{
				return false;
			}
		}
		public static IDataReader UReturnVisitUsabilityDetail_GetByUWCID(int UWCID)
		{
			Database DB = DatabaseFactory.CreateDatabase();

			DbCommand dbCommand = DB.GetStoredProcCommand("UReturnVisitUsabilityDetail_GetByUWCID", UWCID);
			return DB.ExecuteReader(dbCommand);
		}
		public static IDataReader UReturnVisitUsabilityTemp_GetByParId(int ParId)
		{
			Database DB = DatabaseFactory.CreateDatabase();

			DbCommand dbCommand = DB.GetStoredProcCommand("UReturnVisitUsabilityTemp_GetByParId", ParId);
			return DB.ExecuteReader(dbCommand);
		}

	}
}
